package handler.server;

import event.SwordAllMsgEvent;
import event.SwordRegisterEvent;
import event.SwordSendMsgEvent;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import io.netty.channel.ChannelInboundHandlerAdapter;
import server.SwordServe;
import utils.SendUtils;

public class SwordAllMsgEventHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		if(evt instanceof SwordAllMsgEvent){

		}

		if(evt instanceof SwordRegisterEvent){
			Channel channel = ctx.channel();
			ChannelId channelId = channel.id();
			String id = ((SwordRegisterEvent) evt).getId();
			SendUtils.put(id,channelId);
		}

		if(evt instanceof SwordSendMsgEvent){
			String id = ((SwordSendMsgEvent) evt).getId();
			String msg = ((SwordSendMsgEvent) evt).getMsg();
			ChannelId channelId = SendUtils.get(id);
			Channel channel = SwordServe.channelGroup.find(channelId);
			channel.writeAndFlush(msg);
		}
		super.userEventTriggered(ctx, evt);
	}
}
